package com.wy.leetCode;

/**
 * @author HelloWorld
 * @create 2021/10/19 21:01
 * @email 154803771@qq.com
 */

public class LongestCommonPrefix {
    public static void main(String[] args) {
        System.out.println(longestCommonPrefix(new String[]{"aa","aa"}));
    }

    public static String longestCommonPrefix(String[] strs) {
        if (strs.length == 1) {
            return strs[0];
        }
        String result = "";
        String first = strs[0];
        int j = 0;
        for (int i = 1; i < strs.length; ) {
            if (j < strs[i].length() && j < first.length() && strs[i].charAt(j) == first.charAt(j)){
                if (i == strs.length - 1){
                    i = 1;
                    result += first.charAt(j);
                    j++;
                }else {
                    i++;
                }
            }else {
                break;
            }
        }

        return result;
    }
}
